<?php


namespace App\Internals;


use App\Exceptions\InternalServiceException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class CourseInternal
{
    public static function getCourseByCourseIds($courseIds)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses";
            $response = $client->request('GET', $rpcUrl, [
                'query' => ['courseIds' => $courseIds]
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getCourseByCourseId($courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses/{$courseId}";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getCourseCover($courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses/{$courseId}/";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data']['cover'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }
}
